package com.cxy.book.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxy.book.pojo.User;
import com.cxy.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author 蔡心勇
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public ModelAndView login(String username, String password){
        ModelAndView modelAndView=new ModelAndView();
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username).eq("password",password);
        User user = userService.getOne(queryWrapper);
        if(user==null){
            modelAndView.addObject("msg","账号或密码错误，请重新登录");
            modelAndView.setViewName("index");
            modelAndView.addObject("username",username);
        }else {
            modelAndView.setViewName("main");
        }
        return modelAndView;
    }

    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}
